package cn.edu.sdjzu.xg.eagle.bysj.controller;

import java.util.*;
import java.lang.*;

import javax.servlet.http.HttpServletRequest;

import cn.edu.sdjzu.xg.eagle.bysj.entity.TokenEntity;

import cn.edu.sdjzu.xg.eagle.bysj.service.Interface.UsersServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.edu.sdjzu.xg.eagle.bysj.annotation.IgnoreAuth;
import cn.edu.sdjzu.xg.eagle.bysj.annotation.SysLog;

import cn.edu.sdjzu.xg.eagle.bysj.entity.UsersEntity;
import cn.edu.sdjzu.xg.eagle.bysj.entity.view.UsersView;

import cn.edu.sdjzu.xg.eagle.bysj.service.Interface.UsersServiceInterface;
import cn.edu.sdjzu.xg.eagle.bysj.service.Interface.TokenService;
import cn.edu.sdjzu.xg.eagle.bysj.utils.PageUtils;
import cn.edu.sdjzu.xg.eagle.bysj.utils.R;
import cn.edu.sdjzu.xg.eagle.bysj.utils.MPUtil;



@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersServiceInterface usersService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录
     */
    @IgnoreAuth
    @RequestMapping(value = "/login")
    public R login(String username, String password, String captcha, HttpServletRequest request) {
        UsersEntity u = usersService.selectOne(new EntityWrapper<UsersEntity>().eq("username", username));
        if(u==null || !u.getPassword().equals(password)) {
            return R.error("账号或密码不正确");
        }
        String token = tokenService.generateToken(u.getId(), username,"users",  "管理员" );
        return R.ok().put("token", token);
    }

    /**
     * 注册
     */
    @IgnoreAuth
    @RequestMapping("/register")
    public R register(@RequestBody UsersEntity users){
        //ValidatorUtils.validateEntity(users);
        UsersEntity u = usersService.selectOne(new EntityWrapper<UsersEntity>().eq("username", users.getUsername()));
        if(u!=null) {
            return R.error("注册用户已存在");
        }
        Long uId = new Date().getTime();
        users.setId(uId);
        users.setPassword(users.getPassword());
        usersService.insert(users);
        return R.ok();
    }


    /**
     * 退出
     */
    @RequestMapping("/logout")
    public R logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return R.ok("退出成功");
    }

    /**
     * 获取用户的session用户信息
     */
    @RequestMapping("/session")
    public R getCurrUser(HttpServletRequest request){
        Long id = (Long)request.getSession().getAttribute("userId");
        return R.ok().put("data", usersService.selectView(new EntityWrapper<UsersEntity>().eq("id", id)));
    }

    /**
     * 密码重置
     */
    @IgnoreAuth
    @RequestMapping(value = "/resetPass")
    public R resetPass(String username, HttpServletRequest request){
        UsersEntity u = usersService.selectOne(new EntityWrapper<UsersEntity>().eq("username", username));
        if(u==null) {
            return R.error("账号不存在");
        }
        u.setPassword("123456");
        usersService.updateById(u);
        return R.ok("密码已重置为：123456");
    }

    /**
     * 后台列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params,UsersEntity users,
                  HttpServletRequest request){
        EntityWrapper<UsersEntity> ew = new EntityWrapper<UsersEntity>();



        PageUtils page = usersService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, users), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 前端列表
     */
    @IgnoreAuth
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,UsersEntity users,
                  HttpServletRequest request){
        EntityWrapper<UsersEntity> ew = new EntityWrapper<UsersEntity>();

        PageUtils page = usersService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, users), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( UsersEntity users){
        EntityWrapper<UsersEntity> ew = new EntityWrapper<UsersEntity>();
        ew.allEq(MPUtil.allEQMapPre( users, MPUtil.camelToSnake("users")));
        return R.ok().put("data", usersService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(UsersEntity users){
        EntityWrapper< UsersEntity> ew = new EntityWrapper< UsersEntity>();
        ew.allEq(MPUtil.allEQMapPre( users, MPUtil.camelToSnake("users")));
        UsersView usersView =  usersService.selectView(ew);
        return R.ok("查询管理员成功").put("data", usersView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        UsersEntity users = usersService.selectById(id);
        users = usersService.selectView(new EntityWrapper<UsersEntity>().eq("id", id));
        return R.ok().put("data", users);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        UsersEntity users = usersService.selectById(id);
        users = usersService.selectView(new EntityWrapper<UsersEntity>().eq("id", id));
        return R.ok().put("data", users);
    }




    /**
     * 后端保存
     */
    @RequestMapping("/save")
    @SysLog("新增管理员")
    public R save(@RequestBody UsersEntity users, HttpServletRequest request){
        users.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(users);
        UsersEntity u = usersService.selectOne(new EntityWrapper<UsersEntity>().eq("username", users.getUsername()));
        if(u!=null) {
            return R.error("用户已存在");
        }
        users.setId(new Date().getTime());
        users.setPassword(users.getPassword());
        usersService.insert(users);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @SysLog("新增管理员")
    @RequestMapping("/add")
    public R add(@RequestBody UsersEntity users, HttpServletRequest request){
        users.setId(new Date().getTime()+new Double(Math.floor(Math.random()*1000)).longValue());
        //ValidatorUtils.validateEntity(users);
        UsersEntity u = usersService.selectOne(new EntityWrapper<UsersEntity>().eq("username", users.getUsername()));
        if(u!=null) {
            return R.error("用户已存在");
        }
        users.setId(new Date().getTime());
        users.setPassword(users.getPassword());
        usersService.insert(users);
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    @SysLog("修改管理员")
    public R update(@RequestBody UsersEntity users, HttpServletRequest request){
        //ValidatorUtils.validateEntity(users);
        usersService.updateById(users);//全部更新
        if(null!=users.getUsername())
        {
            // 修改token
            TokenEntity tokenEntity = new TokenEntity();
            tokenEntity.setUsername(users.getUsername());
            tokenService.update(tokenEntity, new EntityWrapper<TokenEntity>().eq("userid", users.getId()));
        }
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    @SysLog("删除管理员")
    public R delete(@RequestBody Long[] ids){
        usersService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
}
